package cn.jbolt.core.util;

import java.util.Random;

public class JBoltRandomUtil {
	private static final Random RANDOM = new Random();

	private static String random(char[] baseArr,int baseArrLength,int length) {
		char[] chars = new char[length];
		for (int i = 0; i < length; i++) {
			chars[i]=baseArr[RANDOM.nextInt(baseArrLength)];
		}
		return new String(chars);
	}

	private static final char[] baseArr = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz23456789".toCharArray();
	private static final int baseLength = baseArr.length;
	/**
	 * 随机生成大写字母和数字混合的指定长度字符串
	 *
	 * @param length
	 * @return
	 */
	public static String random(int length) { // length表示生成字符串的长度
		return random(baseArr,baseLength,length);
	}
	private static final char[] baseLowArr = "abcdefghijklmnopqrstuvwxyz".toCharArray();
	private static final int baseLowLength = baseLowArr.length;
	/**
	 * 生成指定长度的字符串 只有小写字母
	 *
	 * @param length
	 * @return
	 */
	public static String randomLow(int length) { // length表示生成字符串的长度生成小写字母sn
		return random(baseLowArr,baseLowLength,length);
	}
	private static final char[] baseUpArr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
	private static final int baseUpLength = baseUpArr.length;
	/**
	 * 生成指定长度的字符串 只有大写字母
	 *
	 * @param length
	 * @return
	 */
	public static String randomUp(int length) { // length表示生成字符串的长度生成大写字母sn
		return random(baseUpArr,baseUpLength,length);
	}

	private static final char[] baseLowWithNumArr = "abcdefghjklmnpqrstuvwxyz23456789".toCharArray();
	private static final int baseLowWithNumLength = baseLowWithNumArr.length;
	/**
	 * 生成指定长度的字符串 小写的带数字
	 *
	 * @param length
	 * @return
	 */
	public static String randomLowWithNumber(int length) {
		return random(baseLowWithNumArr,baseLowWithNumLength,length);
	}


	private static final char[] baseUpWithNumArr = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789".toCharArray();
	private static final int baseUpWithNumLength = baseUpWithNumArr.length;
	/**
	 * 生成指定长度的字符串 大写的带数字
	 *
	 * @param length
	 * @return
	 */
	public static String randomUpWithNumber(int length) {
		return random(baseUpWithNumArr,baseUpWithNumLength,length);
	}

	/**
	 * 生成指定长度数字随机串
	 *
	 * @param length
	 * @return
	 */
	public static Integer randomNumberToInt(int length) { // length表示生成字符串的长度生成小写字母sn
		return Integer.parseInt(randomNumber(length));
	}

	private static final char[] baseNumArr= "0123456789".toCharArray();
	private static final int baseNumArrLen = baseNumArr.length;
	/**
	 * 生成指定长度数字随机串
	 *
	 * @param length
	 * @return
	 */
	public static String randomNumber(int length) { // length表示生成字符串的长度生成小写字母sn
		return random(baseNumArr,baseNumArrLen,length);
	}

}
